package com.sunny.myproject.system.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sunny.myproject.core.base.action.BaseAction;
import com.sunny.myproject.system.entity.Role;
import com.sunny.myproject.system.service.RoleService;

/**
 * @author sunny_nie
 *
 */
@Controller
@RequestMapping("/system/role")
public class RoleAction extends BaseAction<Role>{

	@Autowired
	private RoleService service;

	@Override
	@RequestMapping(params="list")
	public String list(Model model, HttpServletRequest request,
			HttpServletResponse response) {
		super.pager = super.pageStart(model, request);

        super.pager = super.pageEnd(model, service.num(criterions), super.pager);

        super.returnValue(model, service.find(criterions, super.pager.getFirstResults(), super.pager.getMaxResults()));

        return "system/roleList";
	}

	@Override
	@RequestMapping(params="add")
	public String add(HttpServletRequest reuqest, HttpServletResponse response) {
		return "system/roleEdit";
	}

	@Override
	@RequestMapping(params="edit")
	public String edit(Model model, HttpServletRequest request,
			HttpServletResponse response) {
		try {
			super.returnValue(model, service.findById(super.param1(request)));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "system/roleEdit";
	}

	@Override
	@RequestMapping(params="delete")
	public String delete(HttpServletRequest request,
			HttpServletResponse response) {
		try {
			service.deleteByIds(super.param2(request));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "redirect:/system/role?list";
	}

	@Override
	@RequestMapping(params="save")
	public @ResponseBody String save(HttpServletRequest request,
			HttpServletResponse response, Role t) {
		try {
			service.save(t);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "system/role?list";
	}

	@Override
	@RequestMapping(params="view")
	public String view(Model model, HttpServletRequest request,
			HttpServletResponse response) {
		try {
			super.returnViewValue(model, service.findById(super.param1(request)));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "system/roleEdit";
	}

	@RequestMapping(params="query")
	public String query(Model model,HttpServletRequest request){
		try {
			super.returnValue(model, service.findAll());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "system/roleQuery";
	}
}
